package showtime.framework.engine
{
	
	
	/** 
	 * @project showtime
	 * @author yinhao
	 * @date 2011-5-26
	 *
	 */
	
	public class Group2D implements IEntity
	{		
		public var members:Vector.<IEntity> = null;
		
		public function Group2D()
		{
			super();
			
			members = new Vector.<IEntity>();
		}
		
		/**
		 *  Adds a new <code>IGameObject</code> subclass (GameSprite, etc) to the list of children.
		 * 
		 * @param object The object you want to add.
		 * @return The same <code>IGameObject</code> object that was passed in.
		 * 
		 */		
		public function addEntity(entity:IEntity):IEntity
		{
			if (members.indexOf(entity) < 0)
			{
				members[members.length] = entity;
			}
				
			return entity;
		}
		
		/**
		 * Removes an object from the group.
		 *  
		 * @param object The <code>IGameObject</code> you want to remove.
		 * @param splice Whether the object should be cut from the array entirely or not.
		 * @return The removed object.
		 * 
		 */		
		public function removeEntity(entity:IEntity, splice:Boolean = false):IEntity
		{
			var index:int = members.indexOf(entity);
			
			if (index < 0)
			{
				return null;	
			}
			
			if (splice)
			{
				members.splice(index, 1);
			}
			else
			{
				members[index] = null;
			}
			
			return entity;
			
			
		}
		
		/**
		 * Judge is the group include the gameObject. 
		 * @param object
		 * @return 
		 * 
		 */		
		public function containsEntity(entity:IEntity):Boolean
		{
			if (members.indexOf(entity) != -1)
			{
				return true;
			}
			
			return false;
		}
	
		
		public function destroy():void
		{
			for (var index:int = 0, length:int = members.length; index < length; index++)
			{
				var entity:IEntity = members[index];
				entity.destroy();
				
			}
			
		}		
				
		public function update():void
		{
			for (var index:int = 0, length:int = members.length; index < length; index++)
			{
				var entity:Object = members[index];
					
				if (!entity.active)
				{
					continue;
				}
				
				entity.update();
			}
		}
		
		public function render():void
		{
			for (var index:int = 0, length:int = members.length; index < length; index++)
			{
				var entity:Object = members[index];
							
				if (!entity.visible) 
				{
					continue;
				}
							
				entity.render();
							
			}
		}
			
	}
}